package com.eshop.tools.rest;

import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import com.eshop.tools.domain.QiniuContent;
import com.eshop.tools.service.dto.LocalStorageDto;
import org.springframework.http.HttpStatus;
import java.util.HashMap;
import com.eshop.enums.ShopCommonEnum;
import cn.hutool.core.util.StrUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestParam;
import com.eshop.utils.RedisUtils;
import com.eshop.tools.service.QiNiuService;
import com.eshop.tools.service.LocalStorageService;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "上传统一管理" })
@RestController
@RequestMapping({ "/api/upload" })
public class UploadController
{
    private static final Logger log;
    private final LocalStorageService localStorageService;
    private final QiNiuService qiNiuService;
    private final RedisUtils redisUtils;
    
    public UploadController(final LocalStorageService localStorageService, final QiNiuService qiNiuService, final RedisUtils redisUtils) {
        this.localStorageService = localStorageService;
        this.qiNiuService = qiNiuService;
        this.redisUtils = redisUtils;
    }
    
    @ApiOperation("上传文件")
    @PostMapping
    public ResponseEntity<Object> create(@RequestParam(defaultValue = "") final String name, @RequestParam(defaultValue = "") final String type, @RequestParam("file") final MultipartFile[] files) {
        String localUrl = this.redisUtils.getY("admin_api_url");
        if (StrUtil.isBlank((CharSequence)type)) {
            localUrl = this.redisUtils.getY("api_url") + "/api";
        }
        final String mode = this.redisUtils.getY("file_store_mode");
        StringBuilder url = new StringBuilder();
        if (ShopCommonEnum.STORE_MODE_1.getValue().toString().equals(mode)) {
            for (final MultipartFile file : files) {
                final LocalStorageDto localStorageDTO = this.localStorageService.create(name, file);
                if ("".equals(url.toString())) {
                    url = url.append(localUrl + "/file/" + localStorageDTO.getType() + "/" + localStorageDTO.getRealName());
                }
                else {
                    url = url.append("," + localUrl + "/file/" + localStorageDTO.getType() + "/" + localStorageDTO.getRealName());
                }
            }
        }
        else {
            for (final MultipartFile file : files) {
                final QiniuContent qiniuContent = this.qiNiuService.upload(file, this.qiNiuService.find());
                if ("".equals(url.toString())) {
                    url = url.append(qiniuContent.getUrl());
                }
                else {
                    url = url.append("," + qiniuContent.getUrl());
                }
            }
        }
        final Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("errno", 0);
        map.put("link", url);
        return (ResponseEntity<Object>)new ResponseEntity((Object)map, HttpStatus.CREATED);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)UploadController.class);
    }
}
